#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<cerrno>
const uint16_t port = 8088;

int main()
{
    //1.创建socket，打开网络文件
    int sock = socket(AF_INET,SOCK_DGRAM,0);
    if(sock < 0)
    {
        std::cerr<< "socket create faill"<<errno<<std::endl;
        return 1;
    }

    //2.绑定
    sockaddr_in local; 
    local.sin_family = AF_INET;
    local.sin_port = htons(port); //转化为大端
    local.sin_addr.s_addr = INADDR_ANY;

    if(bind(sock,(sockaddr*)&local,sizeof(local))<0)
    {
        std::cerr<<"bind fail"<<errno<<std::endl;
        return 2;
    }
    
    //3.提供服务
    bool quit = false;
    char buff[1024]; //接收数据
    while(!quit)  //需要不断提供服务
    {
        sockaddr_in getmsg;
        socklen_t len = sizeof(getmsg);
        recvfrom(sock,buff,sizeof(buff)-1,0,(sockaddr*)&getmsg,&len);

        std::cout<<"Client#"<<buff<<std::endl;
        std::string _echohello = "hello";
        sendto(sock,_echohello.c_str(),_echohello.size(),0,(sockaddr*)&getmsg,len);
    }

    return 0;
}